package com.sgb.kjwl.viewmodel.home;

import android.content.Intent;
import android.text.TextUtils;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.pick.TenderAllListNewEntity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.pick.PickInfoListEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.swgk.core.view.entity.VideoListEntity;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.sgb.kjwl.model.entity.user.PickCompanyListEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.self.userInfo.SetPwdActivity;
import com.sgb.kjwl.view.ui.fragment.home.PickListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickListFragModel extends BaseViewModel {
    private HomeRepertory repertory;
    private PickListFragment fragment;
    private Map map;
    private Map map2;
    private Map map3;
    private int pageSize = 10;


    public PickListFragModel(PickListFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;

    }

    //招标公告-全部
    public void findNoticeHomeEsPage(int page,String type, String goodsType, String areacode, String city,String keyword) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        Map map = new HashMap();
        map.put("current", page);
        map.put("limit", pageSize);
        if("0".equals(type)){
            type = "";
        }
        map.put("type", type);
        map.put("goodsTypeNo", goodsType);
        map.put("province", areacode);
        map.put("city", city);
        map.put("keyword",keyword);

        APIManager.getInstance().getPickAPI().findNoticeHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListEntity>>> data) {
                if (data == null) return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getAllData(data.getData().getList());
                }
            }
        });
    }

    //招标大厅-全部
    public void findTenderingHomeEsPage(int page,String type, String goodsType, String areacode, String city,String keyword) {

        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "获取信息中...", 0, false, null, false);
        Map map = new HashMap();
        map.put("current", page);
        map.put("limit", 10);
        if("0".equals(type)){
            type = "";
        }
        map.put("type", type);
        map.put("goodsTypeNo", goodsType);
        map.put("province", areacode);
        map.put("city", city);
        map.put("keyword",keyword);

        APIManager.getInstance().getPickAPI().findTenderingHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListEntity>>> data) {
                if (data == null) return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.getAllData(data.getData().getList());
                }
            }
        });
    }

    //竞价
    public void getBiddingList(int page,String typeNo,String goodsType) {
        if (map == null)
            map = new HashMap();
        map.clear();
        map.put("limit", pageSize);
        map.put("current", page);
        if ("0".equals(typeNo)) {
            typeNo = "";
        }
        map.put("type", typeNo);
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsTypeNo", goodsType);
        }
        map.put("queryPosition", "7");
        DialogHelper.showProgressDialog2(fragment.getContext(), null, "数据加载中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().queryHomeEsPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<TenderAllListNewEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<TenderAllListNewEntity>>> data) {
                if (data == null || data.getState() == null) {
                    fragment.showBiddingData(new ArrayList<>());
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        fragment.showBiddingData(data.getData().getList());
                    }
                }
            }
        });
    }

    //招采企业
    public void getCompanyData(int page) {
        Map map = new HashMap();
        map.put("limit", pageSize);
        map.put("page", page);

        DialogHelper.showProgressDialog2(fragment.getContext(), null, "数据加载中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().findTenderCompanyList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<PickCompanyListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<PickCompanyListEntity>>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showCompanyData(data.getData().getList());
                }
            }
        });
    }

    //招标信息
    public void getPickInfoList(int page,String typeNo,String goodsType) {
        Map map = new HashMap();
        map.put("limit", pageSize);
        map.put("page", page);
        if (!TextUtils.isEmpty(typeNo)) {
            if (typeNo.equals("全部")) {
                typeNo = "";
            }
            map.put("tenderType", typeNo);
        }
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsType", goodsType);
        }
        DialogHelper.showProgressDialog2(fragment.getContext(), null, "数据加载中...", 0, false, null, false);
        APIManager.getInstance().getPickAPI().getPickInfoList(map).enqueue(new ICallBack<BaseEntity<ProListEntity<List<PickInfoListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<PickInfoListEntity>>> data) {

                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showPickInfoData(data.getData().getList());
                }

            }
        });

    }

    //中标公告
    public void getBidingData(int page,String type,String goodsType) {

        DialogHelper.showProgressDialog(fragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("limit", pageSize);
        map.put("page", page);
        if(!TextUtils.isEmpty(type) && !type.equals("0")){
            map.put("type",type);
        }
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsType", goodsType);
        }
        APIManager.getInstance().getPickAPI().getBidedList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Response<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showBidingData(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<PreNoticeItemEntity>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });
    }

    public void oneKeyLogin(OneKeyLoginEntity oneKeyLoginEntity) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "登录中", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().oneClickLogin(oneKeyLoginEntity).enqueue(new ICallBack<BaseEntity<LoginEntity>>() {
            @Override
            public void onSuccess(BaseEntity<LoginEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    OneKeyLoginManager.getInstance().finishAuthActivity();
                    if (data.getData().isNeedRegister()) {
                        Intent intent = new Intent(fragment.getActivity(), SetPwdActivity.class);
                        intent.putExtra("mobile", data.getData().getPhone());
                        fragment.getActivity().startActivity(intent);
                    } else {
                        //登录成功逻辑
                        saveUserInfo(data.getData());
                        fragment.loginSucceed(data.getData(), data.getData().getPhone());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, data.getData().getToken());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, data.getData().getPhone());
                        JPushInterface.setAlias(fragment.getActivity(), 1, data.getData().getPhone());
                        getUserInfo();
                    }
                }
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                DialogHelper.dismissProgressDialog();

                if (data == null) return;
                if (data.getData() == null) return;
                if (data.getState().equals("ok")) {
                    fragment.getUserMsgSucceed(data.getData());
                }
            }
        });
    }

    private void saveUserInfo(LoginEntity loginEntity) {
        repertory.saveToken(loginEntity.getToken());
        repertory.saveAccount(loginEntity.getPhone());
    }

    public void getH5Url(String id, String type) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(type).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
//                    String url = "http://192.16limit.2.33:limit067/CompetitivePurchase" + "?id=" + id;
                    fragment.showUrl(url);
                }
            }
        });
    }

    public void userFocus(String businessNo, boolean isGz, int position) {
        Map map = new HashMap();
        map.put("businessNo", businessNo);
        map.put("type", 0);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().userFocus(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setIsGz(isGz, position);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //知识分享接口
    public void memberVideoShare(int page){

        DialogHelper.showProgressDialog(fragment.getContext(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("page",page);
        map.put("limit",pageSize);
        map.put("secondCategory","招标/竞价");
        APIManager.getInstance().getPickAPI().memberVideoShare(map).enqueue(new ICallBack<BaseEntity<VideoListEntity>>() {
            @Override
            public void onSuccess(BaseEntity<VideoListEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showHostVideoData(data.getData().getRecords());
                }
            }
        });
    }

    public void getUrlSetting(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

}
